package com.zq.zqmanager.controller;

import com.alibaba.fastjson.JSONObject;
import com.zq.zqmanager.entity.Cat;
import com.zq.zqmanager.entity.User;
import com.zq.zqmanager.properties.EmailProperties;
import com.zq.zqmanager.service.CatService;
import com.zq.zqmanager.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.*;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.UnauthorizedException;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.EnvironmentAware;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @program: zqmanager
 * @Date: 2020/2/24 16:44
 * @Author: zhengqiang
 * @Description:
 */
@RestController
@Slf4j
public class HelloController implements EnvironmentAware {

    @Autowired
    private CatService catService;

    @Autowired
    private UserService userService;

    @Autowired
    private EmailProperties emailProperties;

    @RequestMapping("/test")
    public String test(){
        return "test";
    }

    @RequestMapping("/getAll")
    public List<Cat> getAll(){
        List<Cat> cats = this.catService.getAll();
        return cats;
    }

    @RequestMapping("/insert")
    public Cat insert(){
        Cat cat = new Cat();
        cat.setCatName("Tomcat");
        cat.setCatAge(2);
        this.catService.insert(cat);
        return cat;
    }

    @RequestMapping("/selectByParams")
    public List<Cat> selectByParams(){
        Cat cat = new Cat();
//        cat.setCatName("Tomcat");
        cat.setCatAge(2);
        List<Cat> cats = this.catService.selectByParams(cat);
        return cats;
    }

    @RequestMapping("/selectUser")
    public List<User> selectUser(){
        return userService.getAll();
    }


    @RequestMapping("/dologin")
    public String login(String username,String password){
        UsernamePasswordToken token = new UsernamePasswordToken(username,password);
        token.setRememberMe(true);
        Subject subject = SecurityUtils.getSubject();
        try {
            subject.login(token);
            subject.getSession().setAttribute("user",username);
            return "success";
        }catch (IncorrectCredentialsException e) {
            System.out.println("登录密码错误");
        } catch (ExcessiveAttemptsException e) {
            System.out.println("登录失败次数过多");
        } catch (LockedAccountException e) {
            System.out.println("帐号已被锁定");
        } catch (DisabledAccountException e) {
            System.out.println("帐号已被禁用");
        } catch (ExpiredCredentialsException e) {
            System.out.println("帐号已过期");
        } catch (UnknownAccountException e) {
            System.out.println("帐号不存在");
        } catch (UnauthorizedException e) {
            System.out.println("您没有得到相应的授权！");
        } catch (Exception e) {

        }
        return "error";
    }


    @Override
    public void setEnvironment(Environment environment) {
        System.out.println(environment.getProperty("JAVA_HOME"));
        System.out.println(environment.getProperty("server.port"));
    }
}
